

part of appcommon;

class DotsIndicatorNormal extends AnimatedWidget{
  const DotsIndicatorNormal({required this.controller, required this.onPageSelected,
    required this.itemCount, this.color = Colors.white, this.unSelectColor = Colors.white,
    this.kDotSize = 8, this.kDotSpacing = 25}): super(listenable: controller);
  /// 页面控制器
  final PageController controller;
  /// 页面数量
  final int itemCount;
  /// 下边改变通知
  final ValueChanged<int> onPageSelected;
  /// 颜色
  final Color color;
  final Color unSelectColor;
  /// 下标大小
  final double kDotSize;
  /// 间距
  final double kDotSpacing;

  @override
  Widget build(BuildContext context) {
    /// 计算界面占位宽度
    double width = MediaQuery.of(context).size.width;
    // TODO: implement build
    return Stack(
        children: [
          Container(child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: List<Widget>.generate(itemCount, (index) {
                return SizedBox(
                    width: kDotSpacing,
                    child: Center(
                        child: Material(
                            color: unSelectColor,
                            type: MaterialType.circle,
                            child: SizedBox(
                                width: kDotSize,
                                height: kDotSize,
                                child: InkWell(
                                    onTap: () => onPageSelected(index)
                                )
                            )
                        )
                    )
                );
              })
          )),
          Positioned(left: (width - kDotSpacing * itemCount) / 2
              + kDotSpacing * (controller.page ?? controller.initialPage), child: SizedBox(
              width: kDotSpacing,
              child: Center(
                  child: Material(
                      color: color,
                      type: MaterialType.circle,
                      child: SizedBox(
                          width: kDotSize, height: kDotSize
                      )
                  )
              )
          ))
        ]
    );
  }
}

class DotsIndicator extends AnimatedWidget {

  const DotsIndicator({required this.controller, this.color = Colors.white,
    required this.itemCount, this.kDotSize = 8.0, this.kDotSpacing = 25.0,
    this.kMaxZoom = 2.0, required this.onPageSelected}): super(listenable: controller);
  /// 页面控制器
  final PageController controller;
  /// 页面数量
  final int itemCount;
  /// 下边改变通知
  final ValueChanged<int> onPageSelected;
  /// 颜色
  final Color color;
  /// 下标大小
  final double kDotSize;
  /// 最大缩放
  final double kMaxZoom;
  /// 间距
  final double kDotSpacing;


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: List<Widget>.generate(itemCount, (index) {
          double selectedness = Curves.easeOut.transform(math.max(0.0, 1.0 - ((controller.page ?? controller.initialPage) - index).abs()));
          double zoom = 1.0 + (kMaxZoom - 1.0) * selectedness;
          return SizedBox(
              width: kDotSpacing,
              child: Center(
                  child: Material(
                      color: color,
                      type: MaterialType.circle,
                      child: SizedBox(
                          width: kDotSize * zoom,
                          height: kDotSize * zoom,
                          child: InkWell(
                              onTap: () => onPageSelected(index)
                          )
                      )
                  )
              )
          );
        })
    );
  }
}